var express = require("express");
var router = express.Router();

// const swperData = require("../data/swper");

let swperData = [
  {
    id: 1,
    url: "https://image4.suning.cn/uimg/cms/img/165044667866216464.jpg?format=is",
  },
  {
    id: 2,
    url: "https://image3.suning.cn/uimg/cms/img/172717123927929482.jpg?format=is",
  },
  {
    id: 3,
    url: "https://image5.suning.cn/uimg/cms/img/170012666707638444.jpg",
  },
  {
    id: 4,
    url: "https://oss.suning.com/adpp/creative_kit/material/picture/20211021-6474f7f341aa4cebbc34583d5c433e54ac69ed66f8374faf.jpg",
  },
];
let id = 6;

/* GET users listing. */
router.get("/list", function (req, res, next) {
  res.json({
    success: true,
    data: swperData,
  });
});

// 新增轮播图
router.post("/add", function (req, res, next) {
  const { url } = req.body;
  if (!url) {
    res.json({
      success: false,
      msg: "参数错误",
    });
  } else {
    swperData.push({
      id: id,
      url,
    });
    id++;
    res.json({
      success: true,
      msg: "新增成功",
    });
  }
});

// 删除轮播图
router.post("/remove", function (req, res, next) {
  const { id } = req.body;
  if (!id) {
    res.json({
      success: false,
      msg: "参数错误",
    });
  } else {
    swperData = swperData.filter((item) => {
      return item.id != id;
    });
    res.json({
      success: true,
      msg: "删除成功",
    });
  }
});

module.exports = router;
